﻿Module Update
    Public Function checkForUpdate() As Integer
        ''This does the quick check online for an update
        Dim sinetXML As String = "http://sinet.sourceforge.net/release.xml"
        Try
            Dim Doc As New XmlDocument
            Doc.Load(sinetXML)
            'Dim Nodes As XmlNodeList
            Dim VersionNode As XmlNode = Doc.SelectSingleNode("//version")
            'Dim URLNode As XmlNode = Doc.SelectSingleNode("//url")
            writeLog(4, "Online Version: " & VersionNode.InnerText, True)
            writeLog(4, "Current Version: " & Application.ProductVersion.Trim, True)
            Dim curVer As Integer = Application.ProductVersion.Replace(".", "")
            Dim newVer As Integer = VersionNode.InnerText.Replace(".", "")
            If curVer < newVer Then
                writeLog(3, "There is an update available!", True)
                ''Ask user if they want to update
                'Dim getAnswer As DialogResult = MessageBox.Show("There is a SiNet update available." & vbNewLine & _
                '            "Would you like to download and install the new version?", "- SiNet Update - ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                'If getAnswer = DialogResult.Yes Then
                ''User wants to update
                'writeLog(3, "Updating...", True)
                'Return launchUpdate(downloadUpdate(URLNode.InnerText))
                'Else
                'writeLog(3, "Skipping update...", True)
                Return 1
                'End If
            End If
        Catch ex As Exception
            MessageBox.Show("checkForUpdate error: " & ex.Message, "- SiNet Error -", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return 0
        End Try
        Return 0
    End Function

    Private Function downloadUpdate(ByVal url As String) As String
        Try
            ''This downloads the update file and returns the path
            Dim fileName As String = url.Substring((url.LastIndexOf("/") + 1), (url.Length - (url.LastIndexOf("/") + 1)))
            If File.Exists(fileName) Then
                'File.Delete(fileName)
                'MessageBox.Show(fileName, "Deleted", MessageBoxButtons.OK)
            End If
            'My.Computer.Network.DownloadFile(url, Application.StartupPath & "/" & fileName)
            Return fileName
        Catch ex As Exception
            MessageBox.Show("downloadUpdate error: " & ex.Message, "- SiNet Error -", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return ""
    End Function

    Private Function launchUpdate(ByVal updatePath As String) As Integer
        ''This function will launch the new installer
        Try
            ''Fire off the setup file and exit the program
            If updatePath.Length > 0 Then
                ''Need something better then this shell command, getting permission denied
                Shell(updatePath, vbNormalFocus)
                Application.Exit()
                Return 0
            Else
                Return 1
            End If
        Catch ex As Exception
            MessageBox.Show("launchUpdate error: " & ex.Message, "- SiNet Error -", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return 1
        End Try
    End Function
End Module
